package com.googlecode.restitory.gae.filter.util;

import org.apache.commons.codec.binary.Base64;

public class CryptoUtil {

	public static String getAuthBasic(String user, String pass) {
		byte[] bytes = new StringBuilder().append(user).append(':').append(pass).toString().getBytes();
		String s = "Basic " + encodeBase64(bytes);
		return s;
	}

	public static String encodeBase64(byte[] bytes) {
		return new Base64(-1).encodeToString(bytes);
	}

	public static String[] decodeBasicAuth(String authorization) {
		String[] split = authorization.split(" ");
		byte[] decoded = decodeBase64(split[1]);
		String str = new String(decoded);
		String[] ret = str.split(":");
		return ret;
	}

	public static byte[] decodeBase64(String str) {
		return Base64.decodeBase64(str);
	}

}
